// ml:run = $bin < input
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node{
    long long x, y, r, c;
}p[2000];
int edge[2000010];
int n;
int e;
int head[2000], ne[2000010];
int DFN[2000], LOW[2000];
long long cost[2000];
int instack[2000], Stap[2000], Belong[2000];
int Bcnt, Stop, Dindex;
int in[2000];
long long cal(int i, int j){
    return (p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y);
}
void init(){
    e=0;
    memset(head, -1, sizeof(head));
    memset(in, 0, sizeof(in));
}
void addnote(int u, int v){
    edge[e]=v;
    ne[e]=head[u];
    head[u]=e++;
}
void tarjan(int i)
{
    int j;
    DFN[i]=LOW[i]=++Dindex;
    instack[i]=true;
    Stap[++Stop]=i;
    for (int now=head[i];now+1;now=ne[now])
    {
        j=edge[now];
        if (!DFN[j])
        {
            tarjan(j);
            if (LOW[j]<LOW[i])
                LOW[i]=LOW[j];
        }
        else if (instack[j] && DFN[j]<LOW[i])
            LOW[i]=DFN[j];
    }
    if (DFN[i]==LOW[i])
    {
        Bcnt++;
        cost[Bcnt]=1000000007;
        do
        {
            j=Stap[Stop--];
            instack[j]=false;
            Belong[j]=Bcnt;
            cost[Bcnt]=min(cost[Bcnt], p[j].c);
        }
        while (j!=i);
    }
}
void solve()
{
    int i;
    Stop=Bcnt=Dindex=0;
    memset(DFN,0,sizeof(DFN));
    for (i=1;i<=n;i++)
        if (!DFN[i])
            tarjan(i);
}
int main()
{
    int t;
    scanf("%d", &t);
    for (int TT=1;TT<=t;TT++){
        init();
        scanf("%d", &n);
        for (int i=1;i<=n;i++)
            scanf("%lld%lld%lld%lld", &p[i].x, &p[i].y, &p[i].r, &p[i].c);
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                if (i!=j)
                    if (cal(i, j)<=p[i].r*p[i].r)
                        addnote(i, j);
        solve();
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                if (i!=j)
                    if (cal(i, j)<=p[i].r*p[i].r && Belong[i]!=Belong[j])
                        in[Belong[j]]++;
        long long ans=0;
        for (int i=1;i<=Bcnt;i++)
            if (in[i]==0)
                ans+=cost[i];
        printf("Case #%d: %lld\n", TT, ans);
    }
}

